package main

import (
	"fmt"
	"gin_project/middleware"
	"gin_project/routers"
	"net/http"

	"github.com/gin-gonic/gin"
)

type Article struct {
	Title string `json:"title"`
	Body  string `json:"body"`
	Age   int    `json:"age"`
}

func main() {

	r := gin.Default()

	r.Use(middleware.PrintRequestInfo())
	r.GET("/", func(c *gin.Context) {

		fmt.Println("hello参数值", c.Query("hello"))
		//c.JSON(net.StatusOK)
		c.String(
			200,
			"hello %v",
			"world")
	})

	r.GET("/users/:id", func(c *gin.Context) {
		id := c.Param("id")
		//c.JSON(net.StatusOK)
		c.JSON(http.StatusOK, gin.H{
			"hello": "world",
			"id":    id,
		})
	})

	r.GET("/json", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"hello": "world",
		})
	})

	r.GET("/jsonMap", func(c *gin.Context) {
		c.JSON(http.StatusOK, map[string]interface{}{
			"hello": "world",
		})
	})

	// 解决跨域
	r.GET("/jsonp", func(c *gin.Context) {

		a := &Article{
			Title: "title",
			Body:  "body",
			Age:   1,
		}

		c.JSONP(http.StatusOK, a)
	})

	r.GET("/xml", func(c *gin.Context) {

		a := &Article{
			Title: "title",
			Body:  "body",
			Age:   1,
		}

		c.XML(http.StatusOK, a)
	})

	r.GET("/beautyJson", func(c *gin.Context) {

		a := &Article{
			Title: "title",
			Body:  "body",
			Age:   1,
		}

		c.IndentedJSON(http.StatusOK, a)
	})

	r.GET("/welcome", func(c *gin.Context) {
		firstname := c.DefaultQuery("firstname", "Guest") // 如果没有，默认为 "Guest"
		lastname := c.Query("lastname")                   // 是 c.Request.URL.Query().Get("lastname") 的快捷方式

		c.String(200, "Hello %s %s", firstname, lastname)
	})

	//r.LoadHTMLGlob("templates/**/*")

	r.StaticFS("/static", http.Dir("static"))

	routers.AdminRoutersInit(r)
	r.Run(":8000")
}
